 /* 2017.3.18 */
 /* www.bkoak.com */
 /* 定时器实时时钟 */

#include <stc15f2k60s2.h>

#define uchar unsigned char
#define uint unsigned int
	
sbit key1=P3^0;//定义端口参数
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
/*
 key1用于小时 +1
 key2用于小时 -1
 key3用于分钟 +1
 key4用于分钟 -1
*/

uchar code tab[] = 
{0XC0,0XF9,0XA4,0XB0,0X99,0X92,
0X82,0XF8,0X80,0X90,0XBF,0XFF};
uchar strtab[8];//定义缓冲区域
uchar hour = 10, minute = 25, second = 12;
//初始化起始时间10-25-12

void display(void);
void delayms(int ms);									
void Timer0Init(void);
void Timer1Init(void);

/*****************************************/
/*****************主函数******************/
/*****************************************/
void main()
{
	P2 = ((P2&0x1f)|0xa0);
	P0 = 0x00;
	
	Timer0Init();
	Timer1Init();
	ET0 = 1;//开启定时器0
	ET1 = 1;//同上
	EA = 1;//定时器使能
	
	display();
	while(1)
	{
		if (!key1)
		{
			delayms(5);//按键消抖
			if (!key1)
			{
				hour++;
				if (hour == 24)
					hour = 0;
				display();
				while(!key1);//松手检测
			}
		}
		
		if (!key2)
		{
			delayms(5);
			if (!key2)
			{
				hour--;
				if (hour == 255)
					hour = 23;
				display();
				while(!key2);
			}
		}
		
		if (!key3)
		{
			delayms(5);
			if (!key3)
			{
				minute++;
				if (minute == 60)
					minute = 0;
				display();
				while(!key3);
			}
		}
		
		if (!key4)
		{
			delayms(5);
			if (!key4)
			{
				minute--;
				if (minute == 255)
					minute = 59;
				display();
				while(!key4);
			}
		}
	}
}

void display(void)
{
	strtab[0] = tab[hour / 10];
	strtab[1] = tab[hour % 10];
	strtab[2] = tab[10];
	strtab[3] = tab[minute / 10];
	strtab[4] = tab[minute % 10];
	strtab[5] = tab[10];
	strtab[6] = tab[second /10];
	strtab[7] = tab[second %10];
}

void delayms(int ms)
{
	int i,j;
	for(i=ms;i>0;i--)
		for(j=845;j>0;j--);
}

void Timer0Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x28;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

void Timer1Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0xCD;		//设置定时初值
	TH1 = 0xD4;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
}

void Timer1_int() interrupt 3 using 0
{
	static uchar num;
	TH1 = 0xcd;
	TL1 = 0xd4;
	
	switch(num)//分别调用缓冲区的值进行扫描
	{
		case 0: P2 = ((P2&0x1f)|0xc0); P0 = 0x01;
					  P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;
		case 1: P2 = ((P2&0x1f)|0xc0); P0 = 0x02;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;
		case 2: P2 = ((P2&0x1f)|0xc0); P0 = 0x04;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		case 3: P2 = ((P2&0x1f)|0xc0); P0 = 0x08;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		case 4: P2 = ((P2&0x1f)|0xc0); P0 = 0x10;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		case 5: P2 = ((P2&0x1f)|0xc0); P0 = 0x20;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		case 6: P2 = ((P2&0x1f)|0xc0); P0 = 0x40;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		case 7: P2 = ((P2&0x1f)|0xc0); P0 = 0x80;
						P2 = ((P2&0x1f)|0xff); P0 = strtab[num];break;;break;
		default:break;
	}
	num++;
	if (num == 8)
		num = 0;
}

void Timer0_int() interrupt 1 using 1
{
	static uchar count;//定义内部静态变量
	TL0 = 0x00;		//初始化初值
	TH0 = 0x28;	
	count++;
	
	switch (count)
	{
		case 0:
		case 40:
		case 80:
		case 120:
		case 160:display();break;	
	}
	if (count == 200)
	{
		count = 0;
		second++;//加一秒
		if (second == 60)
		{
			second = 0;
			minute++;//加一分
			if (minute == 60)
			{
				minute = 0;
				hour++;//加一小时
				if (hour == 24)
					hour = 0;
			}
		}
	}
}